package fr.unice.polytech.chord.gui.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fr.unice.polytech.chord.core.ChordNode;
import fr.unice.polytech.chord.core.Key;
import fr.unice.polytech.chord.core.Tracker;
import fr.unice.polytech.chord.gui.ChordVisualizer;
import fr.unice.polytech.chord.gui.components.JLimitedSizeTextField;

/**
 * Dialogs used in order to remove a {@link ChordNode} on the overlay from the
 * {@link ChordVisualizer} graphical user interface.
 * 
 * @author Anthony Parisy
 * @author Laurent Pellegrino
 * @author Marc Valdener
 * 
 * @version 06/11/2009
 */
@SuppressWarnings("serial")
public class RemoveChordNodeDialog extends ToolBarJDialog implements
		ActionListener {

	private JTextField keyTextField;
	private JButton removeButton;

	public RemoveChordNodeDialog(JFrame parent, JButton toolBarButton) {
		super(parent, "Remove Node", toolBarButton);
	}

	public void createAndShowDialog() {
		super.getContentPane().add(this.createDialog());
		super.pack();
		super.setResizable(false);
		super.setLocationRelativeTo(super.getOwner());
		super.setVisible(true);
	}

	private JPanel createDialog() {
		JPanel container = new JPanel();
		container.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

		super.addWindowListener(new WindowAdapter() {
			@Override
			public void windowDeactivated(WindowEvent e) {
				super.windowDeactivated(e);
				RemoveChordNodeDialog.this.keyTextField.setText("");
			}
		});

		this.removeButton = new JButton("force leave");
		this.removeButton.addActionListener(this);
		this.keyTextField = new JLimitedSizeTextField(10, Key.NB_BITS);
		JLabel keyLabel = new JLabel("Key : ");
		keyLabel.setLabelFor(this.keyTextField);
		this.keyTextField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					RemoveChordNodeDialog.this.removeButton.doClick();
				}
			}
		});
		container.add(keyLabel);
		container.add(this.keyTextField);
		container.add(this.removeButton);
		return container;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		Key key = null;
		try {
			key = new Key(Integer.parseInt(this.keyTextField.getText()));
		} catch (NumberFormatException e) {
			this.keyTextField.setText("");
			this.keyTextField.requestFocus();
			JOptionPane.showMessageDialog(this,
					"Key must be a positive number", "Remove Node",
					JOptionPane.INFORMATION_MESSAGE);
			return;
		}

		Tracker tracker = ((ChordVisualizer) super.getOwner()).getTracker();

		if (tracker.getNumberOfPeersManaged() == 0) {
			JOptionPane.showMessageDialog(this, "No node on the overlay",
					"Remove Node", JOptionPane.INFORMATION_MESSAGE);
			super.setVisible(false);
			return;
		}

		ChordNode node = tracker.getRandomPeer().findSuccessor(key);

		if (node.getKey().compareTo(key) != 0) {
			this.keyTextField.setText("");
			this.keyTextField.requestFocus();
			JOptionPane.showMessageDialog(this, "Node with key " + key
					+ " does not exist on the overlay", "Remove Node",
					JOptionPane.INFORMATION_MESSAGE);
		} else {
			node.leave();
			super.setVisible(false);
			JOptionPane.showMessageDialog(super.getOwner(), "Node with key "
					+ key + " has left the overlay", "Leave Operation",
					JOptionPane.INFORMATION_MESSAGE);
		}

	}
}
